<?php
/*
    Dill is a tool for creating websites with PHP.
    Copyright (C) 2014  Jannik Haberbosch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function db_get_pages( $db )
{
	/* Returns all records of the table 'page'.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an sqlite3 database.

	Return values	Array
					An array that consists of several arrays.  Each of these
					arrays represents a record of the table 'page'.

	*/
	$sqlite3_result = $db->query( "SELECT * FROM page;" );
	$records = array();
	while( ( $row = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) ) != FALSE )
	{
		$records[] = $row;
	}
	return $records;
}


function db_get_containers( $db )
{
	/* Returns all records of the table 'container'.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an sqlite3 database.

	Return values	Array
					An array that consists of several arrays.  Each of these
					arrays represents a record of the table 'container'.

	*/
	$sqlite3_result = $db->query( "SELECT * FROM container;" );
	$records = array();
	while( ( $row = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) ) != FALSE )
	{
		$records[] = $row;
	}
	return $records;
}


function db_get_website_structure( $db, $parent_id,
	$ids_of_branches_to_ignore = array() )
{
	return;
	/* On success, returns a multidimenional array that represents the current
	structure of the website project.
	At the beginning, $parent_id should be NULL to fetch the parent elements.

	This function calls itself recursively.

	On failure, FALSE is returned.
	
	Parameters		$db
					SQLite3 object
					Represents an established connection to an sqlite3 database.

					$parent_id
					integer
					The parent_id of the current record.

					$ids_of_branches_to_ignore
					array
					An array that consists of none, one or more integers.  Each
					of these integers represent an 'id' value.  Branches which
					have parent_id's that match one of these id's will be
					ignored and not be included in the website structure.
					
	Return values	Array or FALSE
					On failure, FALSE is returned.
					
	*/
	// First of all, let's prepare the sqlite3 statement.
	if( $parent_id == NULL )
	{
		if( ( $sqlite3_stmt = $db->prepare( "SELECT * FROM page WHERE parent_id IS NULL ORDER BY sort_id;" ) ) == FALSE )
		{
			// There has been an error preparing the sqlite3 statement.
			return FALSE;
		}
	}
	else
	{
		if( ( $sqlite3_stmt = $db->prepare( "SELECT * FROM page WHERE parent_id = :parent_id ORDER BY sort_id" ) ) == FALSE )
		{
			// There has been an error preparing the sqlite3 statement.
			return FALSE;
		}
		
		// Now bind parameters to the prepared statement.
		$sqlite3_stmt->bindValue( ":parent_id", $parent_id, SQLITE3_INTEGER );		
	}

	// Now execute the sqlite3 statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		// There has been an error when executing the sqlite3 statement.
		$sqlite3_stmt->close();
		return FALSE;
	}


	$arr = array();

	while( ( $result_set = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) ) != FALSE )
	{
		if( !in_array( $result_set["id"], $ids_of_branches_to_ignore ) )
		{
			$arr[$result_set['name']] = $result_set;
		}
	}

	foreach( $arr as &$item )
	{
		if( !in_array( $item["id"], $ids_of_branches_to_ignore ) )
		{
			$item["children"] = db_get_website_structure( $db, $item["id"],
				$ids_of_branches_to_ignore
			);
		}
	}

	return $arr;
}


function db_get_child_pages( $db, $parent_id )
{
	/* This function returns an array that contains the records of all child
	pages of a parent page.  On failure, FALSE is returned.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an SQLite3 database.

					$parent_id
					integer
					The id of the parent record.  Corresponds to the 'id' column.

	Return values	array or FALSE
					An array that consists of several arrays.  Each of these
					arrays represents a record of the table 'page'.
					FALSE is returned on failure.

	*/

	$statement = $db->prepare( "SELECT * FROM page WHERE parent_id = :id ORDER BY sort_id;" );
	if( $statement == FALSE )
	{
		return FALSE;
	}
	$statement->bindValue( ":id", $parent_id, SQLITE3_INTEGER );

	$sqlite3_result = $statement->execute();
	if( $sqlite3_result == FALSE )
	{
		return FALSE;
	}

	$results = array();
	while( ( $record = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) ) != FALSE )
	{
		$results[] = $record;
	}

	return $results;
}


function db_get_parent_pages( $db )
{
	/* This function returns an array that contains the records of all parent
	pages.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an SQLite3 database.

	Return values	array or FALSE
					An array that consists of several arrays.  Each of these
					arrays represents a record of the table 'page'.
					FALSE is returned in case of an error.

	*/
	$statement = $db->prepare( "SELECT * FROM page WHERE parent_id IS NULL ORDER BY sort_id;" );
	if( $statement == FALSE )
	{
		// The sqlite3 statement could not be prepared.
		return FALSE;
	}
	
	$sqlite3_result = $statement->execute();
	if( $sqlite3_result == FALSE )
	{
		// The prepared sqlite3 statement could not be executed.
		$statement->close();
		return FALSE;
	}

	$results = array();
	while( ( $record = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) ) != FALSE )
	{
		$results[] = $record;
	}
	return $results;
}


function db_create_page( $db, $pagename, $template, &$insert_id, $sort_id,
	$parent_id = NULL )
{
	/* On success, this function adds a new record to the 'page' table.
	In case of an error, FALSE is returned.  Otherwise TRUE is returned.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an SQLite3 database.

					$pagename
					string
					The name of the page to create.

					$template
					string
					The name of the template the page uses.

					&$insert_id
					integer
					Will be set in this function.  Will contain the id of the
					record which this function adds to the table 'page'.

					$sort_id
					integer
					Tells where the page will appear in the website structure.

					$parent_id
					integer
					The parent id of the record to add.  Useful when creating
					branches.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/
	if( $parent_id == NULL )
	{
		// The record to create will represent a parent page.
		if( ( $statement = $db->prepare( "INSERT INTO page (
			name,
			template,
			sort_id ) VALUES (
			:name,
			:template,
			:sort_id
		)" ) ) == FALSE )
		{
			// There has been an error creating the sqlite3 statement.
			return FALSE;
		}

		$statement->bindValue( ":name", $pagename, SQLITE3_TEXT );
		$statement->bindValue( ":template", $template, SQLITE3_TEXT );
		$statement->bindValue( ":sort_id", $sort_id, SQLITE3_INTEGER );

		$sqlite3_result = $statement->execute();
	}
	else
	{
		if( ( $statement = $db->prepare( "INSERT INTO page (
			name,
			template,
			parent_id,
			sort_id
			) VALUES (
			:name,
			:template,
			:parent_id,
			:sort_id )" ) ) == FALSE )
		{
			// There has been an error preparing the sqlite3 statement.
			return FALSE;
		}

		// Bind values.
		$statement->bindValue( ":name", $pagename, SQLITE3_TEXT );
		$statement->bindValue( ":template", $template, SQLITE3_TEXT );
		$statement->bindValue( ":parent_id", $parent_id, SQLITE3_INTEGER );
		$statement->bindValue( ":sort_id", $sort_id, SQLITE3_INTEGER );

		// Execute the sql statement.
		$sqlite3_result = $statement->execute();	
	}

	if( $sqlite3_result instanceof SQLite3Result )
	{
		$insert_id = $db->lastInsertRowID();
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


function db_create_containers( $db, $template_containers, $page_id )
{
	/* This function creates the relevant containers for a single page.
	On success, TRUE is returned; otherwise FALSE.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$template_containers
					array
					An array of strings, each of which defines a container.

					$page_id
					id
					All containers that are created belong to this page.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/
	if( ( $sqlite3_stmt = $db->prepare(
		"INSERT INTO container
		( name, content, page_id )
		VALUES
		( :template_container, :content, :page_id )"
	) ) == FALSE )
	{
		// There has been an error preparing the statement.
		return FALSE;
	}

	foreach( $template_containers as $container )
	{
		if( count( $container ) == 0 ) continue; // TEMPORARY BUGFIX!
		// Now bind values to the parameters of the prepared statement.	
		$sqlite3_stmt->bindValue( ":template_container", $container, SQLITE3_TEXT );
		$sqlite3_stmt->bindValue( ":content", "", SQLITE3_TEXT );
		$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );

		// Now execute the statement.
		if( $sqlite3_stmt->execute() == FALSE )
		{
			// There has been an error when executing the SQLite3 statement.
			return FALSE;
		}
	}

	// The containers for the page have successfully been created.
	$sqlite3_stmt->close();

	return TRUE;
}


function db_get_parent_id_by_parent_name( $db, $parent_name )
{
	/* This function returns the id of the parent element that has been selected
	in the wxTreeCtrl.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$parent_name
					string
					The name of the parent page.
				
	Return values	Integer/FALSE
					The id of the parent is returned on success.  On failure,
					FALSE is returned.

	*/
	if( ( $sqlite3_stmt = $db->prepare(
		"SELECT *
		FROM page
		WHERE name = :parent_name
		AND parent_id IS NULL"
	)) == FALSE )
	{
		// An error has occured while trying to create an sqite3 statement.
		return FALSE;
	}
	else
	{
		// Bind values to the sqlite3 statement.
		$sqlite3_stmt->bindValue( ":parent_name", $parent_name, SQLITE3_TEXT );

		// Now try to execute the statement.
		if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
		{
			// There has been an error when trying to execute the prepared statement.
			$sqlite3_stmt->close();
			return FALSE;
		}
		else
		{
			$record = $sqlite3_result->fetchArray( SQLITE3_ASSOC );
			$sqlite3_result->finalize();
			$sqlite3_stmt->close();

			return $record["id"];
		}
	}
}


function db_get_child_id_by_parent_id_and_child_name( $db, $parent_id, $child_name )
{
	/* This function returns the id of a parent record by finding it using the
	name of the child record and the id of the parent.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$parent_id
					integer
					The id of the parent page.

					$child_name
					string
					The name of the child page.

	Return values	Array/FALSE
					TRUE is returned on success; FALSE on failure.					

	*/
	if( ( $sqlite3_stmt = $db->prepare(
		"SELECT *
		FROM page
		WHERE parent_id = :parent_id
		AND name = :child_name;"
	)) == FALSE )
	{
		// An error has occured while trying to create the sqlite3 statement.
		return FALSE;
	}

	// Now bind parameters.
	$sqlite3_stmt->bindValue( ":parent_id", $parent_id, SQLITE3_INTEGER );
	$sqlite3_stmt->bindValue( ":child_name", $child_name, SQLITE3_TEXT );

	// Now try to execute the statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		// An error has occured when trying to execute the statement.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		$result_record = $sqlite3_result->fetchArray( SQLITE3_ASSOC );
		$sqlite3_result->finalize();
		$sqlite3_stmt->close();

		return $result_record["id"];
	}
}


function db_rename_page( $db, $page_id, $new_pagename )
{
	/* This function returns 'TRUE', if the 'name' column of a record inside the
	table 'page' could be updated successfully.  Otherwise 'FALSE' is returned.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					The id of the page to rename.

					$new_pagename
					string
					The new name for the page.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.					
	*/

	// Prepare the SQLite statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE page
		SET name=:new_page_name
		WHERE id=:page_id;"
	) ) == FALSE )
	{
		// An error has occured when trying to prepare an sqlite3 statement.
		return FALSE;
	}

	// Bind parameters.
	$sqlite3_stmt->bindValue( ":new_page_name", $new_pagename, SQLITE3_TEXT );
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );

	// Now execute the statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		/* An error has occured while trying to update the 'name' column of a
		record. */
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		$sqlite3_result->finalize();
		$sqlite3_stmt->close();
		return TRUE;
	}
}


function db_delete_page( $db, $id )
{
	/* This function deletes all records from the table 'page' which are either
	children of a parent record and/or the parent record itself.  The '$id'
	variable contains the 'id' value of the parent which is matched against the
	column 'id' inside the table 'page'.
	On success, 'TRUE' is returned, otherwise 'FALSE'.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$id
					integer
					The id of the record (page) to delete.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/

	$child_pages = db_get_child_pages( $db, $id );
	$result = NULL;
	foreach( $child_pages as $cp )
	{
		$result = db_delete_page( $db, $cp["id"] );
		if( $result == FALSE )
		{
			return FALSE;
		}
	}

	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"DELETE FROM page
		WHERE id=:id"
	) ) == FALSE )
	{
		// An error has occured when trying to prepare an sqlite3 statement.
		return FALSE;
	}

	// Now bind parameters.
	$sqlite3_stmt->bindValue( ":id", $id, SQLITE3_INTEGER );

	// Now execute the sqlite3 statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		// An error has occured when trying to delete a record of the table 'page'.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		// The record has successfully been deleted of the table 'page'.
		$sqlite3_result->finalize();
		$sqlite3_stmt->close();
		return TRUE;
	}
}


function db_reverse_two_items( $db, $item_to_move_up_id, $item_to_move_down_id )
{
	/* Reverses the order of two items, each of which represents a page.
	On success, TRUE is returned, otherwise FALSE is returned.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$item_to_move_up_id
					id
					The id of the item to move up.					

					$item_to_move_down_id
					id
					The id of the item to move down.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/

	// Prepare the sqlite3 statements.
	if( ( $sqlite3_stmt1 = $db->prepare(
		"UPDATE page
		SET sort_id = sort_id - 1
		WHERE id = :item_to_move_up_id;"
	) ) == FALSE )
	{
		// An error has occured when trying to prepare an sqlite3 statement.
		return FALSE;
	}

	if( ( $sqlite3_stmt2 = $db->prepare(
		"UPDATE page
		SET sort_id = sort_id + 1
		WHERE id = :item_to_move_down_id;"
	) ) == FALSE )
	{
		// An error has occured when trying to prepare an sqlite3 statement.
		return FALSE;
	}

	// Now bind parameters to both statements :D.
	$sqlite3_stmt1->bindValue( ":item_to_move_up_id", $item_to_move_up_id, SQLITE3_INTEGER );
	$sqlite3_stmt2->bindValue( ":item_to_move_down_id", $item_to_move_down_id, SQLITE3_INTEGER );

	// And now try to execute both statements.
	if( $sqlite3_stmt1->execute() == FALSE )
	{
		/* There has been an error when trying to execute the prepared sqlite3
		statement. */
		$sqlite3_stmt1->close();
		return FALSE;
	}

	if( $sqlite3_stmt2->execute() == FALSE )
	{
		/* There has been an error when trying to execute the prepared sqlite3
		statement. */
		$sqlite3_stmt2->close();
		return FALSE;
	}

	return TRUE;
}


function db_page_refresh_sort_id( $db, $id, $sort_id )
{
	/* Re-sets the value of the column 'sort_id' for a certain row in the table
	'page'.  On success, TRUE is returned.  On failure, FALSE is returned.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$id
					integer
					The page of which the sort id needs to be changed.

					$sort_id
					id
					Contains the new sort id.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/

	// Prepare the SQLite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE page
		SET sort_id = :sort_id
		WHERE id = :id;"
	) ) == FALSE )
	{
		/* There has been an error when trying to prepare an sqlite3 statement.
		*/
		return FALSE;
	}

	// Now bind parameters to the prepared sqlite3 statement =).
	$sqlite3_stmt->bindValue( ":sort_id", $sort_id, SQLITE3_INTEGER );
	$sqlite3_stmt->bindValue( ":id", $id, SQLITE3_INTEGER );

	// Now execute the prepared sqlite3 statement.
	if( $sqlite3_stmt->execute() == FALSE )
	{
		/* An error has occured when trying to update the value for the column
		'sort_id' in the table 'page' for a certain record. */
		$sqlite3_stmt->close();
		return FALSE;
	}
	return TRUE;
}


function db_page_move_page( $db, $new_sort_id, $id, $new_parent_id )
{
	/* Re-sets the value of the column "parent_id" for a certain row in the table
	'page'.  The row is identified by the '$id'.  Also resets the "sort_id".
	On success, TRUE is returned.  On failure, FALSE is returned.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$new_sort_id
					integer
					The new sort id for the page.

					$id
					integer
					The id of the page to which to assign the new sort id and
					the new parent id.

					$new_parent_id
					integer

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE page
		SET parent_id = :parent_id, sort_id = :sort_id
		WHERE id = :id;"
	) ) == FALSE )
	{
		/* There has been an error when trying to prepare the sqlite3 statement.
		*/
		return FALSE;
	}

	// Now bind parameters to the prepared sqlite3 statement.
	$sqlite3_stmt->bindValue( ":parent_id", $new_parent_id, SQLITE3_INTEGER );
	$sqlite3_stmt->bindValue( ":sort_id", $new_sort_id, SQLITE3_INTEGER );
	$sqlite3_stmt->bindValue( ":id", $id, SQLITE3_INTEGER );

	// Now try to execute the prepared sqlite3 statement.
	if( $sqlite3_stmt->execute() == FALSE )
	{
		/* An error has occured when trying to execute the prepared sqlite3
		statement. */
		$sqlite3_stmt->close();
		return FALSE;
	}

	$sqlite3_stmt->close();
	return TRUE;
}


function db_container_delete( $db, $page_id, $container_name )
{
	/* Deletes a row (container), identyfing it by using the 'id' and the 'name'
	of the container.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					The container to be deleted belongs to the page with the id
					'$page_id'.

					$container_name
					string
					The name of the container to be deleted.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.	

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"DELETE FROM 'container'
		WHERE page_id = :page_id AND name = :container_name"
	) ) == FALSE )
	{
		// There has been an error preparing the sqlite3 statement.
		return FALSE;
	}

	// Bind values to the sqlite3 statement.
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );
	$sqlite3_stmt->bindValue( ":container_name", $container_name, SQLITE3_TEXT );

	// Now try to execute the prepared sqlite3 statement.
	if( ( $sqlite3_stmt->execute() ) == FALSE )
	{
		// An error has occured when trying to delete a certain record.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}


function db_container_create( $db, $page_id, $container_name )
{
	/* Creates a new container (record) in the table 'container'.  Relationship
	to a page is done by setting the value for the column 'page_id' to '$page_id'.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					The container belongs to the page with that page id.

					$container_name
					string
					The name of the container to create.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.							

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"INSERT INTO container ( name, page_id )
		VALUES ( :container_name, :page_id )"
	) ) == FALSE )
	{
		// The sqlite3 statement could not be prepared.
		return FALSE;
	}

	// Bind values to the sqlite3 statement.
	$sqlite3_stmt->bindValue( ":container_name", $container_name, SQLITE3_TEXT );
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );

	// Now try to execute the prepared sqlite3 statement.
	if( ( $sqlite3_stmt->execute() ) == FALSE )
	{
		// An error has occured when trying to insert a new record.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		$sqlite3_stmt->close();
		return TRUE;
	}
}


function db_container_get_by_page_id(
	$db,
	$page_id
)
{
	/* Returns an array that contains the container data for a single page by
	comparing the value of the column 'page_id' to identify the associated
	containers that belong to a page.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					Get the container names of containers that belong to the
					page with the page id '$page_id'.

	Return values	Array/FALSE
					An array is returned on success; FALSe on failure.
					The array contains the names of the containers that belong
					to this page.

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"SELECT *
		FROM container
		WHERE page_id = :page_id"
	) ) == FALSE )
	{
		// The sqlite3 statement could not be prepared.
		return FALSE;
	}
	
	// Bind values to the prepared sqlite3 statement.
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );
	
	// Try to execute the sqlite3 statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		// There has been an error executing the prepared sqlite3 statement.
		$sqlite3_stmt->close();
		return FALSE;
	}

	$records = array();
	while( $row = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) )
	{
		$records[] = $row["name"];
	}

	$sqlite3_result->finalize();
	$sqlite3_stmt->close();
	return $records;
}


function db_container_get_by_page_id2(
	$db,
	$page_id
)
{
	/* Returns an array that contains the container data for a single page by
	comparing the value of the column 'page_id' to identify the associated
	containers that belong to a page.


	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					Return containers that belong to the page with the id '$page_id'.

	Return values	Array/FALSE
					An array is returned on success; FALSE on failure.						

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"SELECT *
		FROM container
		WHERE page_id = :page_id"
	) ) == FALSE )
	{
		// The sqlite3 statement could not be prepared.
		return FALSE;
	}
	
	// Bind values to the prepared sqlite3 statement.
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );
	
	// Try to execute the sqlite3 statement.
	if( ( $sqlite3_result = $sqlite3_stmt->execute() ) == FALSE )
	{
		// There has been an error executing the prepared sqlite3 statement.
		$sqlite3_stmt->close();
		return FALSE;
	}

	$records = array();
	while( $row = $sqlite3_result->fetchArray( SQLITE3_ASSOC ) )
	{
		$records[$row["name"]] = $row;
	}

	$sqlite3_result->finalize();
	$sqlite3_stmt->close();
	return $records;
}


function db_page_change_template(
	$db,
	$page_id,
	$chosen_template_name
)
{
	/* This function changes the template of a page.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$page_id
					integer
					The id of a page of which the template should be changed.

					$chosen_template_name
					string
					The name of the new template that is assigned to this record.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.	

	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE page
		SET template = :template
		WHERE id = :page_id"
	) ) == FALSE )
	{
		// The sqlite3 statement could not be prepared.
		return FALSE;
	}

	// Bind values to the prepared sqlite3 statement.
	$sqlite3_stmt->bindValue( ":template", $chosen_template_name, SQLITE3_TEXT );
	$sqlite3_stmt->bindValue( ":page_id", $page_id, SQLITE3_INTEGER );

	// Execute the prepared sqlite3 statement.
	if( $sqlite3_stmt->execute() == FALSE )
	{
		// An error has occured while trying to execute the prepaed sqlite3 statement.
		$sqlite3_stmt->close();
		return FALSE;
	}
	return TRUE;
}


function db_container_update_content(
	$db,
	$id,
	$content
)
{
	/* Updates the column 'content' of a certain record in the table 'container'.
	Returns FALSE on failure, TRUE on success.

	Parameters		$db
					SQLite3 object.
					Represents an established connection to an SQLite3 database.

					$id
					integer
					The id of the container to update.

					$content
					string
					The content to write into the column "content" of the table
					"container".

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.
						
	*/
	// Prepare the sqlite3 statement.
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE container
		SET content = :content
		WHERE id = :id;"
	) ) == FALSE )
	{
		// There has been an error preparing the sqlite3 statement.
		return FALSE;
	}


	// Bind values to the prepared sqlite3 statement.
	$sqlite3_stmt->bindValue( ":content", $content, SQLITE3_TEXT );
	$sqlite3_stmt->bindValue( ":id", $id, SQLITE3_INTEGER );

	// Try to execute the sqlite3 statement.
	if( $sqlite3_stmt->execute() == FALSE )
	{
		// There has been an error executing the sqlite3 statement.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		$sqlite3_stmt->close();
		return TRUE;
	}
}


function db_page_rename_template( $db, $old_template_name, $new_template_name )
{
	/* Updates the column 'template' of every record in the table 'page', where
	the value of the column 'template' matches '$old_template_name' to
	'$new_template_name'.

	On success, TRUE is returned.  On failure, FALSE is returned.

	Parameters		$db
					SQLite3 object
					Represents an established connection to an SQLite3 database.

					$old_template_name
					string
					The old template name of the template that has just been
					renamed.

					$new_template_name
					string
					The new template name to which the template has just been
					renamed to.

	Return values	TRUE/FALSE
					TRUE is returned on success; FALSE on failure.

	*/
	// Prepare the sqlite3 statement.
	echo $new_template_name;
	echo PHP_EOL;
	echo $old_template_name;
	if( ( $sqlite3_stmt = $db->prepare(
		"UPDATE page
		SET template = :new_template_name
		WHERE template = :old_template_name;"
	) ) == FALSE )
	{
		// Error:  The sqlite3 statement could not be prepared.
		return FALSE;
	}

	// Bind values.
	$sqlite3_stmt->bindValue( "new_template_name", $new_template_name, SQLITE3_TEXT );
	$sqlite3_stmt->bindValue( "old_template_name", $old_template_name, SQLITE3_TEXT );

	// Execute the prepared sqlite3 statement.
	if( $sqlite3_stmt->execute() == FALSE )
	{
		// Error:  The prepared sqlite3 statement could not be executed.
		$sqlite3_stmt->close();
		return FALSE;
	}
	else
	{
		// The affected records have been updated.
		$sqlite3_stmt->close();
		return TRUE;
	}
}


?>
